#!/bin/sh

set -e

gnumex_ver=2.06

wgetcmd=wget
wgetcount=`which wget 2>/dev/null | wc -l`
if test ! $wgetcount = 1; then
  echo "Utility wget not found in your PATH."
  if test `uname` = Darwin; then
    wgetcmd=ftp
    echo "Using ftp command instead."
  elif test `uname` = FreeBSD; then
    wgetcmd=fetch
    echo "Using fetch command instead."
  else
    exit -1
  fi
fi

echo " "
echo "Running script to download the source code for Gnumex $gnumex_ver."
echo " "

rm -f gnumex*.zip

echo "Downloading the source code from downloads.sourceforge.net..."
$wgetcmd http://sourceforge.net/projects/gnumex/files/Older-versions/gnumex${gnumex_ver}.zip

if test -d gnumex ; then
  echo "Moving current gnumex to gnumex.OLD."
  if test -d gnumex.OLD ; then
    rm -rf gnumex.OLD
  fi
  mv gnumex gnumex.OLD
fi

echo "Uncompressing the zip..."
unzip gnumex${gnumex_ver}.zip
mv trunk gnumex

echo " "
echo "Done downloading the source code for gnumex."
echo "Modifying permissions of exe and mex files."
chmod +x gnumex/*.exe gnumex/*.mexw32

echo " "
echo "Verify that there are no error messages in the output above."
